package main

import (
	"log"
	"os"
	"text/template"
)

func main() {

	t := template.Must(template.ParseGlob("./views/*.tpl"))

	titles := []string{"标题1", "标题2", "标题3"}

	//根据参数u, 渲染命名为demo的模板，并且将渲染结果打印到标准输出
	err := t.ExecuteTemplate(os.Stdout, "demo", titles)
	if err != nil {
		log.Println("executing template:", err)
	}

	dataMap := map[string]string{}
	dataMap["key1"] = "value1"
	dataMap["key2"] = "value2"

	err = t.ExecuteTemplate(os.Stdout, "map", dataMap)
	if err != nil {
		log.Println("executing template:", err)
	}

	type Data struct {
		Titles []string
	}

	//初始化模版参数
	data := Data{Titles: []string{"标题1", "标题2", "标题3"}}

	err = t.ExecuteTemplate(os.Stdout, "struct", data)
	if err != nil {
		log.Println("executing template:", err)
	}




}
